#!/bin/bash
#
# Basic build environment for the P2P project..
#
# example run:
#   mikkel@laptop:/tmp/p2p$ ./build.sh clean build run
#   CLEANING OUTPUT FOLDER './bin' ...
#   ....
#
#
# TODO:
# - get the Peer's startup data from file
#   requires:
#     IO to/from files/pipes/sockets instead of STDIN/STDOUT
#

# Global conf variables
# *********************

SRC_DIR=./src/com
LIB_DIR=./lib
BIN_DIR=./bin
MAIN_CLASS=com.Peer
PM=com.PuppetMaster
# locate all .jar libraries and populate the string $LIBS with the filenames
for f in $LIB_DIR/*.jar; do
    # if $LIBS isn't declared yes, libs=$f, else libs=$libs:$f
    if [ -n $LIBS ]
        then LIBS="$LIBS:$f";  # NOTE: on windows replace ':' with ';'
        else LIBS="$f";  # first time
    fi
done


# Build functions
# ***************

function clean(){
    # Cleans the build-output directory
    rm -rf $BIN_DIR/*
}

function build(){
    # Compiles .java source to .class bytecode, linking the libraries
    echo "    javac -d ./bin -sourcepath ./src -cp $LIBS $SRC_DIR/*.java"
    echo
    javac -d ./bin -sourcepath ./src -cp $LIBS $SRC_DIR/*.java
}

function run(){
    # run the com.Peer class, including the libraries in the class path
    echo "java -cp $BIN_DIR:$LIBS $MAIN_CLASS $1 $2 $3 $4"
    echo
    java -cp $BIN_DIR:$LIBS $MAIN_CLASS $1 $2 $3 $4 
}

function pm() {
    # run the puppet master
    java -cp $BIN_DIR:$LIBS $PM $1 $2 $3
}

function killall(){
    # kill all java processes running an instance of $MAIN_CLASS

    PIDS=`ps aux|grep "$MAIN_CLASS"|grep -v grep`             # all processes with "java" in their name
    while [ -n "$PIDS" ]; do
        NODES=`ps aux|grep "$MAIN_CLASS"|grep -v grep|wc -l`  # count how many lines ps|grep returns
        echo "    need to kill $NODES nodes";

        PID=`ps aux|grep $MAIN_CLASS | awk '{ print $2 }'`;   # grep the PID 
        if [ $NODES != 0 ]; then
            `kill -9 $PID` 012>/dev/null                      # send SIGKILL to the PID with -9
            wait $PID 012>/dev/null;                          # wait for the process to die
        fi;
        PIDS=`ps aux|grep "$MAIN_CLASS"|grep -v grep`;        # refresh PIDs
    done
}


# Build script entry point
# ************************

# parse arguments through a switch-case
for ARG in "$@";    # $@ = array of args
do
    case "$ARG" in
        build)      echo "BUILDING SOURCE FROM '$SRC_DIR' INTO '$BIN_DIR' ..." ; build ;;
        clean)      echo "CLEANING OUTPUT FOLDER '$BIN_DIR' ..." ; clean ;;
        run)        echo "RUNNING PROJECT..." ; run $4 $5 $6 $7 ;;
        pm)         echo "RUNNING PUPPET MASTER..." ; pm $4 $5 $6 ;;
        killall)    echo "KILLING ALL RUNNING NODES..." ; killall ;;
        # default case:
        *)          echo
                    echo "command '$ARG' was not understood"
                    echo
                    echo "available commands:"
                    echo "    build, clean, run"
                    echo ;;
    esac
    echo
    echo
done

